
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>第九章：函数 ·  GO语言 web服务器区块链开发工程师就业班课程</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="波波">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-sectionx/sectionx.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-codeblock-filename/block.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-navigator/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../Chapter10/工程管理.html" />
    
    
    <link rel="prev" href="../Chapter8/流程控制.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://www.shxdledu.cn/single/About-Me" target="_blank" class="custom-link">关于作者</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../Chapter1/base.html">
            
                <a href="../Chapter1/base.html">
            
                    
                    第一章：区块链基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../Chapter2/base.html">
            
                <a href="../Chapter2/base.html">
            
                    
                    第二章：初识Go语言
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../Chapter2/GO语言介绍.html">
            
                <a href="../Chapter2/GO语言介绍.html">
            
                    
                    第1节：GO语言介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../Chapter2/环境安装.html">
            
                <a href="../Chapter2/环境安装.html">
            
                    
                    第2节：环境安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../Chapter2/第一个GO程序.html">
            
                <a href="../Chapter2/第一个GO程序.html">
            
                    
                    第3节：第一个GO程序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../Chapter2/学习资料.html">
            
                <a href="../Chapter2/学习资料.html">
            
                    
                    第4节：学习资料
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../Chapter3/注释.html">
            
                <a href="../Chapter3/注释.html">
            
                    
                    第三章：注释
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../Chapter3/注释作用.html">
            
                <a href="../Chapter3/注释作用.html">
            
                    
                    第1节：注释作用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../Chapter3/注释分类.html">
            
                <a href="../Chapter3/注释分类.html">
            
                    
                    第2节：注释分类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../Chapter3/学习资料.html">
            
                <a href="../Chapter3/学习资料.html">
            
                    
                    第3节：学习资料
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../Chapter4/变量.html">
            
                <a href="../Chapter4/变量.html">
            
                    
                    第四章：变量
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../Chapter4/什么叫变量.html">
            
                <a href="../Chapter4/什么叫变量.html">
            
                    
                    第1节：什么叫变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../Chapter4/变量类型.html">
            
                <a href="../Chapter4/变量类型.html">
            
                    
                    第2节：变量类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../Chapter4/变量声明.html">
            
                <a href="../Chapter4/变量声明.html">
            
                    
                    第3节：变量声明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../Chapter4/变量初始化.html">
            
                <a href="../Chapter4/变量初始化.html">
            
                    
                    第4节：变量初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../Chapter4/变量赋值.html">
            
                <a href="../Chapter4/变量赋值.html">
            
                    
                    第5节：变量赋值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../Chapter4/自动推导类型.html">
            
                <a href="../Chapter4/自动推导类型.html">
            
                    
                    第6节：自动推导类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="../Chapter4/多重赋值匿名变量.html">
            
                <a href="../Chapter4/多重赋值匿名变量.html">
            
                    
                    第7节：多重赋值匿名变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="../Chapter4/数据置换.html">
            
                <a href="../Chapter4/数据置换.html">
            
                    
                    第8节：数据置换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9" data-path="../Chapter4/输出格式.html">
            
                <a href="../Chapter4/输出格式.html">
            
                    
                    第9节：输出格式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.10" data-path="../Chapter4/接收输入.html">
            
                <a href="../Chapter4/接收输入.html">
            
                    
                    第10节：接收输入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.11" data-path="../Chapter4/变量命名规范.html">
            
                <a href="../Chapter4/变量命名规范.html">
            
                    
                    第11节：变量命名规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.12" data-path="../Chapter4/变量命名规范.html">
            
                <a href="../Chapter4/变量命名规范.html">
            
                    
                    第12节：变量命名规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../Chapter5/基础数据类型.html">
            
                <a href="../Chapter5/基础数据类型.html">
            
                    
                    第五章：基础数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../Chapter6/常量.html">
            
                <a href="../Chapter6/常量.html">
            
                    
                    第六章：常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../Chapter7/运算符.html">
            
                <a href="../Chapter7/运算符.html">
            
                    
                    第七章：运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../Chapter8/流程控制.html">
            
                <a href="../Chapter8/流程控制.html">
            
                    
                    第八章：流程控制
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.10" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    第九章：函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../Chapter10/工程管理.html">
            
                <a href="../Chapter10/工程管理.html">
            
                    
                    第十章：工程管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../Chapter11/数据格式.html">
            
                <a href="../Chapter11/数据格式.html">
            
                    
                    第十一章：数据格式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../Chapter12/面向对象.html">
            
                <a href="../Chapter12/面向对象.html">
            
                    
                    第十二章：面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../Chapter13/异常处理.html">
            
                <a href="../Chapter13/异常处理.html">
            
                    
                    第十三章：异常处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../Chapter14/文本文件处理.html">
            
                <a href="../Chapter14/文本文件处理.html">
            
                    
                    第十四章：文本文件处理
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >第九章：函数</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x7B2C;&#x4E5D;&#x7AE0;&#xFF1A;&#x51FD;&#x6570;">&#x7B2C;&#x4E5D;&#x7AE0;&#xFF1A;&#x51FD;&#x6570;</h1>
<h2 id="1&#xFF1A;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x548C;&#x4F7F;&#x7528;">1. 1&#xFF1A;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x548C;&#x4F7F;&#x7528;</h2>
<pre class="language-"><code>package main

import &quot;fmt&quot;

//func &#x51FD;&#x6570;&#x540D;(&#x53C2;&#x6570;&#x5217;&#x8868;)(&#x8FD4;&#x56DE;&#x503C;&#x5217;&#x8868;){
//&#x4EE3;&#x7801;&#x4F53;
//}
//&#x51FD;&#x6570;&#x5B9A;&#x4E49; &#x53EA;&#x80FD;&#x5B9A;&#x4E49;&#x4E00;&#x6B21;
//&#x5728;&#x6574;&#x4E2A;&#x9879;&#x76EE;&#x4E2D;&#x51FD;&#x6570;&#x540D;&#x662F;&#x552F;&#x4E00;&#x7684; &#x4E0D;&#x80FD;&#x91CD;&#x540D;
func add(s1 int, s2 int) {
    sum := s1 + s2
    fmt.Println(sum)
}

func main() {
    //fmt.Println(&quot;&#x4F60;&#x597D;&quot;)
    //v := len(&quot;&#x4F60;&#x597D;&quot;)
    //fmt.Println(v)
    a := 10
    b := 20

    //&#x51FD;&#x6570;&#x8C03;&#x7528; &#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x591A;&#x6B21;&#x8C03;&#x7528;
    //&#x5728;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x65F6;&#x53C2;&#x6570;&#x4E3A;&#x5B9E;&#x9645;&#x53C2;&#x6570;&#xFF08;&#x5B9E;&#x53C2;&#xFF09;&#x6709;&#x5177;&#x4F53;&#x7684;&#x503C; &#x7528;&#x6765;&#x7ED9;&#x5F62;&#x5F0F;&#x53C2;&#x6570;&#xFF08;&#x5F62;&#x53C2;&#xFF09;&#x4F20;&#x9012;&#x6570;&#x636E;
    add(a, b)
    add(1, 2)
}
</code></pre><h2 id="2&#xFF1A;&#x666E;&#x901A;&#x53C2;&#x6570;&#x5217;&#x8868;">2. 2&#xFF1A;&#x666E;&#x901A;&#x53C2;&#x6570;&#x5217;&#x8868;</h2>
<p>&#x6240;&#x8C13;&#x7684;&#x666E;&#x901A;&#x53C2;&#x6570;&#x5217;&#x8868;&#x6307;&#x7684;&#x662F;&#xFF0C;&#x6211;&#x4EEC;&#x7ED9;&#x51FD;&#x6570;&#x4F20;&#x9012;&#x7684;&#x53C2;&#x6570;&#x7684;&#x4E2A;&#x6570;&#x90FD;&#x662F;&#x786E;&#x5B9A;&#x597D;&#x3002;&#x57FA;&#x672C;&#x8BED;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<p>&#x4EE3;&#x7801;&#x5728;&#x4E0A;&#x9762;</p>
<h2 id="3&#xFF1A;&#x4E0D;&#x5B9A;&#x53C2;&#x6570;&#x5217;&#x8868;">3. 3&#xFF1A;&#x4E0D;&#x5B9A;&#x53C2;&#x6570;&#x5217;&#x8868;</h2>
<p>&#x6839;&#x636E;&#x524D;&#x9762;&#x7684;&#x8BB2;&#x89E3;&#xFF0C;&#x6211;&#x4EEC;&#x90FD;&#x77E5;&#x9053;&#x4E86;&#xFF0C;&#x5728;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#x6839;&#x636E;&#x9700;&#x6C42;&#x6307;&#x5B9A;&#x53C2;&#x6570;&#x7684;&#x4E2A;&#x6570;&#x548C;&#x7C7B;&#x578B;&#xFF0C;&#x4F46;&#x662F;&#x6709;&#x65F6;&#x5019;&#x5982;&#x679C;&#x65E0;&#x6CD5;&#x786E;&#x5B9A;&#x53C2;&#x6570;&#x7684;&#x4E2A;&#x6570;&#x5462;&#xFF1F;
&#x4E3E;&#x4F8B;&#x8BF4;&#x660E;&#xFF1A;&#x4E0A;&#x4E00;&#x5C0F;&#x8282;&#x6211;&#x4EEC;&#x5199;&#x8FC7;&#x4E00;&#x4E2A;&#x6C42;&#x4E24;&#x4E2A;&#x6574;&#x6570;&#x4E4B;&#x548C;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x4F46;&#x662F;&#x5728;&#x5B9E;&#x9645;&#x7684;&#x5F00;&#x53D1;&#x4E2D;&#xFF0C;&#x4E5F;&#x4F1A;&#x7ECF;&#x5E38;&#x9047;&#x5230;&#x8FD9;&#x6837;&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x5C31;&#x662F;&#x9879;&#x76EE;&#x7ECF;&#x7406;&#xFF08;&#x5BF9;&#x6574;&#x4E2A;&#x9879;&#x76EE;&#x7684;&#x8FDB;&#x5EA6;&#x8FDB;&#x884C;&#x628A;&#x63A7;&#xFF0C;&#x5BF9;&#x7A0B;&#x5E8F;&#x5458;&#x8FDB;&#x884C;&#x7BA1;&#x7406;&#x7684;&#x4EBA;&#x5458;&#xFF0C;&#x79F0;&#x4E3A;&#x9879;&#x76EE;&#x7ECF;&#x7406;&#xFF09;&#xFF0C;&#x8981;&#x6C42;&#x4F60;&#x5199;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x5B9E;&#x73B0;&#x6574;&#x6570;&#x7684;&#x548C;&#x3002;&#x5728;&#x8FD9;&#x4E2A;&#x8981;&#x6C42;&#x4E2D;&#xFF0C;&#x9879;&#x76EE;&#x7ECF;&#x7406;&#x5E76;&#x6CA1;&#x6709;&#x8BF4;&#x6E05;&#x695A;&#x5230;&#x5E95;&#x662F;&#x6709;&#x51E0;&#x4E2A;&#x6574;&#x6570;&#xFF0C;&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x5E94;&#x8BE5;&#x600E;&#x6837;&#x786E;&#x5B9A;&#x8BE5;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x5462;&#xFF1F;&#x5C31;&#x7528;&#x63A5;&#x4E0B;&#x6765;&#x7ED9;&#x5927;&#x5BB6;&#x8BB2;&#x89E3;&#x7684;&#x201C;&#x4E0D;&#x5B9A;&#x53C2;&#x6570;&#x5217;&#x8868;&#x201D;&#x6765;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;</p>
<pre class="language-"><code>package main

import &quot;fmt&quot;

//...&#x4E0D;&#x5B9A;&#x53C2; &#x5728;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x65F6;&#x53EF;&#x4EE5;&#x4F20;&#x9012;&#x4E0D;&#x5B9A;&#x91CF;&#xFF08;0-n&#xFF09;&#x7684;&#x53C2;&#x6570;
//&#x4E0D;&#x5B9A;&#x53C2;&#x4F7F;&#x7528;&#x6570;&#x636E;&#x683C;&#x5F0F;&#x4E3A;&#x5207;&#x7247;
func sum(arr ...int) {
    //arr&#x662F;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x7684;&#x96C6;&#x5408;
    //fmt.Println(arr)

    //&#x8BA1;&#x7B97;&#x5B57;&#x7B26;&#x4E32;&#x4E2A;&#x6570;
    //len(&quot;&#x4F60;&#x597D;&quot;)
    //&#x8BA1;&#x7B97;&#x6570;&#x636E;&#x96C6;&#x5408;&#x7684;&#x4E2A;&#x6570;
    //count := len(arr)
    //fmt.Println(count)

    //&#x901A;&#x8FC7;arr[&#x4E0B;&#x6807;]&#x53EF;&#x4EE5;&#x627E;&#x5230;&#x5177;&#x4F53;&#x6570;&#x636E;&#x7684;&#x503C;
    //&#x4E0B;&#x6807;&#x662F;&#x4ECE;0&#x5F00;&#x59CB;&#x7684;  &#x5230;len(arr)-1&#x4E3A;&#x6700;&#x5927;&#x503C;&#x4E0B;&#x6807;
    //fmt.Println(arr[0])
    //fmt.Println(arr[1])
    //fmt.Println(arr[2])
    //fmt.Println(arr[3])

    sum := 0
    //&#x901A;&#x8FC7;for&#x5FAA;&#x73AF;&#x904D;&#x5386;&#x96C6;&#x5408;&#x4E2D;&#x7684;&#x6570;&#x636E;
    //for i := 0; i &lt; len(arr); i++ {
    //    //fmt.Println(arr[i])
    //    sum += arr[i]
    //}
    //&#x901A;&#x8FC7;for&#x5FAA;&#x73AF;&#x904D;&#x5386;&#x96C6;&#x5408;&#x4E2D;&#x7684;&#x6570;&#x636E;
    //i -&gt; index &#x4E0B;&#x6807; v -&gt; value &#x503C;
    //_&#x533F;&#x540D;&#x53D8;&#x91CF;  &#x5982;&#x679C;&#x6570;&#x636E;&#x7684;&#x503C;&#x4E0D;&#x9700;&#x8981;&#x63A5;&#x6536;  &#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x533F;&#x540D;&#x53D8;&#x91CF;&#x6765;&#x63A5;&#x6536;&#x6570;&#x636E;
    for _, v := range arr {
        //fmt.Println(i, v)
        sum += v
    }
    fmt.Println(sum)
}
func main() {
    //&#x4E0D;&#x5B9A;&#x53C2;&#x51FD;&#x6570;&#x8C03;&#x7528;
    sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    sum(12, 21, 23, 32)

    //fmt.Println &#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x4E0D;&#x5B9A;&#x53C2;&#x7684;&#x683C;&#x5F0F;
    fmt.Println(&quot;&#x4F60;&#x597D;&quot;, 123, &quot;&#x518D;&#x89C1;&quot;)
}
</code></pre><h2 id="4:_&#x51FD;&#x6570;&#x5D4C;&#x5957;&#x8C03;&#x7528;">4. 4: &#x51FD;&#x6570;&#x5D4C;&#x5957;&#x8C03;&#x7528;</h2>
<p>&#x51FD;&#x6570;&#x4E5F;&#x53EF;&#x4EE5;&#x50CF;&#x6211;&#x4EEC;&#x5728;&#x524D;&#x9762;&#x5B66;&#x4E60;if&#x9009;&#x62E9;&#x7ED3;&#x6784;,for&#x5FAA;&#x73AF;&#x7ED3;&#x6784;&#x4E00;&#x6837;&#x8FDB;&#x884C;&#x5D4C;&#x5957;&#x4F7F;&#x7528;&#x3002;&#x6240;&#x8C13;&#x51FD;&#x6570;&#x7684;&#x5D4C;&#x5957;&#x4F7F;&#x7528;&#xFF0C;&#x5176;&#x5B9E;&#x5C31;&#x662F;&#x5728;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x4E2D;&#x8C03;&#x7528;&#x53E6;&#x5916;&#x7684;&#x51FD;&#x6570;&#x3002;</p>
<pre class="language-"><code>package main

import &quot;fmt&quot;

func test1(a int, b int) {

    fmt.Println(a + b)
}

//&#x51FD;&#x6570;&#x53C2;&#x6570;&#x4F20;&#x9012;&#x65F6;&#x5982;&#x679C;&#x6709;&#x591A;&#x4E2A;&#x53C2;&#x6570; &#x4E2D;&#x95F4;&#x7528;&#x9017;&#x53F7;&#x5206;&#x9694;
//&#x5728;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x65F6; &#x4F7F;&#x7528;&#x7684;&#x662F;&#x5B9A;&#x4E49;&#x4E2D;&#x5F62;&#x53C2;&#x7684;&#x503C;
func test(n1, n2 int) {
    test1(n1, n2)

}

func main0401() {
    a := 10
    b := 20
    test(a, b)
}

func test3(arr ...int) {

    fmt.Println(arr)
}

//&#x5982;&#x679C;&#x4E0D;&#x5B9A;&#x53C2;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x65F6;&#x4F20;&#x9012;&#x7684;&#x53C2;&#x6570;&#x4E3A;&#x591A;&#x4E2A; &#x4E0D;&#x5B9A;&#x53C2;&#x8981;&#x5199;&#x5728;&#x5176;&#x4ED6;&#x53C2;&#x6570;&#x540E;&#x9762;
func test2(arr ...int) {
    //&#x4E0D;&#x80FD;&#x5C06;&#x4E0D;&#x5B9A;&#x53C2;&#x7684;&#x540D;&#x79F0;&#x4F20;&#x9012;&#x7ED9;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x4E0D;&#x5B9A;&#x53C2;
    //test3(arr[0], arr[1], arr[2], arr[3])
    //&#x4F20;&#x9012;&#x6307;&#x5B9A;&#x4E2A;&#x6570;&#x7684;&#x6570;&#x636E;
    test3(arr[0:4]...)
}

//&#x4E0D;&#x5B9A;&#x53C2;&#x51FD;&#x6570;&#x7684;&#x5D4C;&#x5957;&#x8C03;&#x7528;
func main() {
    test2(1, 2, 3, 4)
}
</code></pre><h2 id="5:_&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;">5. 5: &#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;</h2>
<pre class="language-"><code>package main

import &quot;fmt&quot;

//func &#x51FD;&#x6570;&#x540D;(&#x51FD;&#x6570;&#x53C2;&#x6570;&#x5217;&#x8868;)(&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;)
func test4(a int, b int) (sum int) {
    sum = a + b
    //sum := a + b
    //fmt.Println(&quot;hello world&quot;)
    //return &#x8868;&#x793A;&#x51FD;&#x6570;&#x7684;&#x7ED3;&#x675F;  &#x5982;&#x679C;&#x51FD;&#x6570;&#x6709;&#x8FD4;&#x56DE;&#x503C;return &#x53EF;&#x4EE5;&#x5C06;&#x8FD4;&#x56DE;&#x503C;&#x8FD4;&#x56DE;
    return
    //fmt.Println(&quot;&#x4F60;&#x7785;&#x5565;&quot;)
    //fmt.Println(&quot;&#x7785;&#x4F60;&#x548B;&#x5730;&quot;)
}
func main0501() {
    a := 10
    b := 20
    //var sum int = 0
    sum := test4(a, b)
    fmt.Println(sum)

}

//&#x591A;&#x4E2A;&#x8FD4;&#x56DE;&#x503C;
func test5(a int, b int) (sum int, sub int) {

    sum = a + b
    sub = a - b
    return
}
func main() {

    a := 10
    b := 20

    //&#x51FD;&#x6570;&#x6709;&#x591A;&#x4E2A;&#x8FD4;&#x56DE;&#x503C; &#x8981;&#x4E00;&#x4E00;&#x5BF9;&#x5E94;&#x63A5;&#x6536;&#x6570;&#x636E;
    sum, sub := test5(a, b)

    fmt.Println(sum, sub)
}
</code></pre><h2 id="6:_&#x51FD;&#x6570;&#x7C7B;&#x578B;">6. 6: &#x51FD;&#x6570;&#x7C7B;&#x578B;</h2>
<pre class="language-"><code>package main

import &quot;fmt&quot;

func demo1(a int, b int) {
    fmt.Println(a + b)
}

//func (int,int)int&#x51FD;&#x6570;&#x7C7B;&#x578B;
func demo2(s1 int, s2 int) {
    fmt.Println(s1 + s2)
}

//&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x7C7B;&#x578B; &#x4E3A;&#x5DF2;&#x5B58;&#x5728;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x8D77;&#x522B;&#x540D;
type FUNCDEMO func(int, int)

func main() {
    //demo1(10, 20)
    //&#x51FD;&#x6570;&#x7684;&#x540D;&#x5B57;&#x8868;&#x793A;&#x4E00;&#x4E2A;&#x5730;&#x5740; &#x51FD;&#x6570;&#x5728;&#x4EE3;&#x7801;&#x533A;&#x7684;&#x5730;&#x5740;
    fmt.Println(demo1)

    //f&#x662F;func (int,int)&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x5B9A;&#x4E49;&#x7684;&#x53D8;&#x91CF;
    //var f func(int, int)
    var f FUNCDEMO
    //&#x51FD;&#x6570;&#x7C7B;&#x578B;&#x5B9A;&#x4E49;&#x7684;&#x53D8;&#x91CF;&#x7ED3;&#x675F;&#x51FD;&#x6570;&#x7684;&#x5730;&#x5740;
    f = demo1
    fmt.Println(f)
    //&#x901A;&#x8FC7;f&#x8C03;&#x7528;&#x51FD;&#x6570;
    f(10, 20)

    f = demo2
    f(1, 2)

    fmt.Printf(&quot;%T&quot;, f)
}
</code></pre><h2 id="7:_07&#x51FD;&#x6570;&#x7684;&#x4F5C;&#x7528;&#x57DF;">7. 7: 07&#x51FD;&#x6570;&#x7684;&#x4F5C;&#x7528;&#x57DF;</h2>
<pre class="language-"><code>package main

import &quot;fmt&quot;

//&#x5728;&#x51FD;&#x6570;&#x5916;&#x90E8;&#x5B9A;&#x4E49;&#x7684;&#x53D8;&#x91CF;  &#x6210;&#x4E3A;&#x5168;&#x5C40;&#x53D8;&#x91CF;
//&#x4F5C;&#x7528;&#x57DF;&#x662F;&#x5728;&#x9879;&#x76EE;&#x4E2D;&#x6574;&#x4E2A;&#x6587;&#x4EF6;&#x53BB;&#x4F7F;&#x7528;
//&#x5B9A;&#x4E49;&#x7684;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x540D;&#x4E0D;&#x80FD;&#x548C;&#x5176;&#x4ED6;&#x6587;&#x4EF6;&#x4E2D;&#x7684;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x91CD;&#x540D;
//&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x540D;&#x53EF;&#x4EE5;&#x548C;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x540D;&#x91CD;&#x540D;
//&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x50A8;&#x5B58;&#x5728;&#x5185;&#x5B58;&#x7684;&#x6570;&#x636E;&#x533A;
//&#x5982;&#x679C;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x5B9A;&#x4E49;&#x65F6;&#x6709;&#x503C; &#x5B58;&#x50A8;&#x5728;&#x521D;&#x59CB;&#x5316;&#x6570;&#x636E;&#x533A; &#x6CA1;&#x6709;&#x503C;&#x5B58;&#x50A8;&#x5728;&#x4E3A;&#x521D;&#x59CB;&#x5316;&#x6570;&#x636E;&#x533A;
var a int = 10

func main0701() {
    //&#x53D8;&#x91CF;&#x5148;&#x5B9A;&#x4E49;&#x540E;&#x4F7F;&#x7528; &#x5728;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x53D8;&#x91CF;&#x540D;&#x662F;&#x552F;&#x4E00;&#x7684;
    //&#x5728;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x5B9A;&#x4E49;&#x7684;&#x53D8;&#x91CF; &#x6210;&#x4E3A;&#x5C40;&#x90E8;&#x53D8;&#x91CF;
    //&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x5728;&#x51FD;&#x6570;&#x5185;&#x90E8;

    //a := 10
    //fmt.Println(a)
    //var i int = 10
    //for i = 0; i &lt; 5; i++ {
    //    fmt.Println(i)
    //}
    //fmt.Println(i)

    //&#x5728;go&#x8BED;&#x8A00;&#x4E2D;&#x4F1A;&#x91C7;&#x7528;&#x5C31;&#x8FDB;&#x539F;&#x5219; &#x5982;&#x679C;&#x5728;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x5B9A;&#x4E49;&#x5C40;&#x90E8;&#x53D8;&#x91CF; &#x548C;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x540D;&#x91CD;&#x540D; &#x4F1A;&#x4F7F;&#x7528;&#x5C40;&#x90E8;&#x53D8;&#x91CF;
    a := 123

    //&#x4FEE;&#x6539;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x7684;&#x503C;
    //&#x5982;&#x679C;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x7684;&#x503C;&#x4FEE;&#x6539; &#x4F1A;&#x5F71;&#x54CD;&#x5176;&#x4ED6;&#x4F4D;&#x7F6E;&#x4F7F;&#x7528;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x7684;&#x503C;
    a = 110
    fmt.Println(a)

    demo3()
}

func demo3() {
    fmt.Println(a)
}

func main() {

    //&#x6253;&#x5370;&#x4EE3;&#x7801;&#x533A;&#x7684;&#x5730;&#x5740;   &#x4EE3;&#x7801;&#x533A;
    fmt.Println(demo3)
    //&#x6253;&#x5370;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x7684;&#x5730;&#x5740;  &#x6570;&#x636E;&#x533A;
    fmt.Println(&amp;a)
    a := 10
    //&#x6253;&#x5370;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x7684;&#x5730;&#x5740;  &#x6808;&#x533A;
    fmt.Println(&amp;a)
}
</code></pre><h2 id="8:_08&#x533F;&#x540D;&#x51FD;&#x6570;">8. 8: 08&#x533F;&#x540D;&#x51FD;&#x6570;</h2>
<pre class="language-"><code>package main

import &quot;fmt&quot;

type FuncType func(int, int)

func main0801() {

    a := 10
    b := 20

    //&#x5728;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x533F;&#x540D;&#x51FD;&#x6570;
    //f &#x51FD;&#x6570;&#x7C7B;&#x578B;&#x53D8;&#x91CF; &#x63A5;&#x6536;&#x51FD;&#x6570;

    var f FuncType

    f = func(a int, b int) {
        fmt.Println(a + b)
    }

    f(a, b)
    //fmt.Println(f)

}
func main0802() {
    //a := 10
    //b := 20
    // int&#x7C7B;&#x578B;
    //v := func(a int, b int) int {
    //    return a + b
    //}(a, b)

    //&#x5982;&#x679C;&#x5B9A;&#x4E49;&#x533F;&#x540D;&#x51FD;&#x6570;&#x540E;&#x5728;{}&#x540E;&#x9762;&#x5982;&#x679C;&#x6709;()&#x8868;&#x793A;&#x51FD;&#x6570;&#x8C03;&#x7528;
    f := func(a int, b int) int {
        return a + b
    }

    v := f(10, 20)
    fmt.Printf(&quot;%T\n&quot;, f)
    fmt.Println(v)
}
func main0803() {
    a := 10
    b := 20

    f := func() int {
        return a + b
    }

    //v := f(a, b)

    a = 100
    b = 200

    v := f()

    fmt.Println(v)
}
</code></pre><h2 id="9:_09&#x9012;&#x5F52;&#x51FD;&#x6570;">9. 9: 09&#x9012;&#x5F52;&#x51FD;&#x6570;</h2>
<pre class="language-"><code>package main

import &quot;fmt&quot;

//&#x9012;&#x5F52;&#x51FD;&#x6570;  &#x51FD;&#x6570;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x672C;&#x8EAB;
//&#x6B7B;&#x9012;&#x5F52;
func demo4(n int) {

    //&#x5728;&#x51FD;&#x6570;&#x4E2D;&#x9047;&#x5230;return &#x8868;&#x793A;&#x51FD;&#x6570;&#x7684;&#x7ED3;&#x675F;
    if n == 0 {
        return
    }

    demo4(n - 1)
    fmt.Println(n)
}

func main0901() {

    demo4(10)

}

//&#x8BA1;&#x7B97;&#x4E00;&#x4E2A;&#x6570;&#x7684;&#x9636;&#x4E58;
var s int = 1

func demo5(n int) {
    if n == 1 {
        return
    }
    s *= n//5*4*3*2
    demo5(n - 1)


}

func main() {

    a := 5
    demo5(a)

    fmt.Println(s)

}
</code></pre><footer class="page-footer"><span class="copyright">Copyright &#xA9; shxdledu.cn 2018 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2019-11-07 00:57:20
</span></footer><div id="anchors-navbar"><i class="fa fa-anchor"></i><ul><p><a href="#&#x7B2C;&#x4E5D;&#x7AE0;&#xFF1A;&#x51FD;&#x6570;">&#x7B2C;&#x4E5D;&#x7AE0;&#xFF1A;&#x51FD;&#x6570;</a></p><li><a href="#1&#xFF1A;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x548C;&#x4F7F;&#x7528;">1. 1&#xFF1A;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x548C;&#x4F7F;&#x7528;</a></li><li><a href="#2&#xFF1A;&#x666E;&#x901A;&#x53C2;&#x6570;&#x5217;&#x8868;">2. 2&#xFF1A;&#x666E;&#x901A;&#x53C2;&#x6570;&#x5217;&#x8868;</a></li><li><a href="#3&#xFF1A;&#x4E0D;&#x5B9A;&#x53C2;&#x6570;&#x5217;&#x8868;">3. 3&#xFF1A;&#x4E0D;&#x5B9A;&#x53C2;&#x6570;&#x5217;&#x8868;</a></li><li><a href="#4:_&#x51FD;&#x6570;&#x5D4C;&#x5957;&#x8C03;&#x7528;">4. 4: &#x51FD;&#x6570;&#x5D4C;&#x5957;&#x8C03;&#x7528;</a></li><li><a href="#5:_&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;">5. 5: &#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;</a></li><li><a href="#6:_&#x51FD;&#x6570;&#x7C7B;&#x578B;">6. 6: &#x51FD;&#x6570;&#x7C7B;&#x578B;</a></li><li><a href="#7:_07&#x51FD;&#x6570;&#x7684;&#x4F5C;&#x7528;&#x57DF;">7. 7: 07&#x51FD;&#x6570;&#x7684;&#x4F5C;&#x7528;&#x57DF;</a></li><li><a href="#8:_08&#x533F;&#x540D;&#x51FD;&#x6570;">8. 8: 08&#x533F;&#x540D;&#x51FD;&#x6570;</a></li><li><a href="#9:_09&#x9012;&#x5F52;&#x51FD;&#x6570;">9. 9: 09&#x9012;&#x5F52;&#x51FD;&#x6570;</a></li></ul></div><a href="#1&#xFF1A;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x548C;&#x4F7F;&#x7528;" id="goTop"><i class="fa fa-arrow-up"></i></a>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../Chapter8/流程控制.html" class="navigation navigation-prev " aria-label="Previous page: 第八章：流程控制">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../Chapter10/工程管理.html" class="navigation navigation-next " aria-label="Next page: 第十章：工程管理">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"第九章：函数","level":"1.10","depth":1,"next":{"title":"第十章：工程管理","level":"1.11","depth":1,"path":"Chapter10/工程管理.md","ref":"Chapter10/工程管理.md","articles":[]},"previous":{"title":"第八章：流程控制","level":"1.9","depth":1,"path":"Chapter8/流程控制.md","ref":"Chapter8/流程控制.md","articles":[]},"dir":"ltr"},"config":{"plugins":["toggle-chapters","sectionx","tbfed-pagefooter","include-codeblock","splitter","mermaid","codeblock-filename","prism","-highlight","simple-page-toc","navigator"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy shxdledu.cn 2018","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"prism":{},"simple-page-toc":{"maxDepth":3,"skipFirstH1":true},"splitter":{},"search":{},"mermaid":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"sectionx":{},"codeblock-filename":{},"navigator":{},"include-codeblock":{"check":false,"edit":false,"fixlang":false,"lang":"","template":"default","theme":"chrome","unindent":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":true,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"toggle-chapters":{}},"theme":"default","author":"波波","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":" GO语言 web服务器区块链开发工程师就业班课程","language":"zh-hans","links":{"sidebar":{"关于作者":"http://www.shxdledu.cn/single/About-Me"}},"gitbook":"*","description":" GO语言 web服务器区块链开发工程师就业班课程"},"file":{"path":"Chapter9/函数.md","mtime":"2019-11-06T16:57:20.326Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-06T17:01:48.340Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sectionx/sectionx.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

